import { getCookieValue, setCookieValue } from '@/utils/utils'
import i18n, { LANGS, getLanguage, getLanguageItem } from '@/lang'
import { changeLanguage } from '@/api/settings'
import { getToken } from '@/utils/auth/token'
import { configuration } from '@/api/app'
import { getDictList } from '@/api/business/dict'
import defaultSettings from '@/settings'
import abp from 'abp'
import _ from 'lodash'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'

const state = {
  sidebar: {
    opened: getCookieValue('sidebarStatus') ? !!+getCookieValue('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  size: getCookieValue('size') || 'small',

  app: {
    siteName: defaultSettings.title,
    siteLogo: defaultSettings.logo,
    description: ""
  },
  token: getToken(),
  language: getLanguage(),
  dicts: []
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      setCookieValue('sidebarStatus', 1)
    } else {
      setCookieValue('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    setCookieValue('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    setCookieValue('size', size)
  },
  SET_APP: (state, app) => {
    state.app.siteName = app.siteName || defaultSettings.title
    state.app.siteLogo = app.siteLogo || defaultSettings.logo
    state.app.description = app.description
  },
  SET_LANGUAGE: (state, language) => {
    state.language = language
    setCookieValue(
      'localization_language',
      language,
      new Date(new Date().getTime() + 5 * 365 * 86400000),
      '/'
    );
  },
  SET_DICTS: (state, dict) => {
    state.dicts = dict;
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },

  configuration({ commit }) {
    return new Promise((resolve, reject) => {
      configuration().then((data) => {
        _.merge(abp, data)
        abp.log.level = abp.log.levels.WARN;
        // 设置app相关
        commit('SET_APP', { siteName: abp.setting.get("app.site.name"), siteLogo: abp.setting.get("app.site.logo"), description: "" })

        // 设置语言相关
        var language = abp.setting.get("app.localization.default_language");

        commit('SET_LANGUAGE', language) // 先从设置里获取语言
        const item = getLanguageItem(state.language)
        i18n.locale = item.vue;
        dayjs.locale(item.dayjs)

        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  setLanguage({ commit, state }, lang) {
    return new Promise((resolve, reject) => {
      var language = lang
      var languageName = LANGS[lang].abp

      if (state.token) {
        changeLanguage({ languageName: languageName.trim() }).then(() => {
          commit('SET_LANGUAGE', language)
          resolve()
        }).catch(error => {
          reject(error)
        })
      } else {
        commit('SET_LANGUAGE', language)
        resolve()
      }
    })
  },
  getDict({ commit }) {
    return new Promise((resolve, reject) => {
      getDictList().then((data) => {
        commit('SET_DICTS', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
